#include <windows.h>
#include <tchar.h>
#include "UIEdit.h"

const TCHAR *lpszClassName = TEXT("EditCtrl");
CMyEdit *pMyEdit = NULL;
const RECT rc  = {100, 100, 500, 500};

LRESULT CALLBACK WinProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

int WINAPI WinMain( __in HINSTANCE hInstance, __in_opt HINSTANCE hPrevInstance, __in LPSTR lpCmdLine, __in int nShowCmd )
{
	WNDCLASS wndClass;
	wndClass.cbClsExtra = 0;
	wndClass.cbWndExtra = 0;
	wndClass.hbrBackground = (HBRUSH)::GetStockObject(WHITE_BRUSH);
	wndClass.hCursor = LoadCursor(hInstance, MAKEINTRESOURCE(IDC_ARROW));
	wndClass.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
	wndClass.hInstance = hInstance;
	wndClass.lpfnWndProc = WinProc;
	wndClass.lpszClassName = lpszClassName;
	wndClass.lpszMenuName = NULL;
	wndClass.style = CS_VREDRAW | CS_HREDRAW | CS_DBLCLKS;

	if(!RegisterClass(&wndClass))
	{
		MessageBox(NULL,TEXT("RegisterClass failed!"),TEXT("Notice"), MB_OK);
		return -1;
	}

	HWND hWnd = NULL;
	hWnd = CreateWindow(lpszClassName, TEXT("test"), WS_OVERLAPPEDWINDOW,
		rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top, NULL, NULL, hInstance, 0);

	if(!hWnd)
	{
		MessageBox(NULL,TEXT("CreateWindow failed!"),TEXT("Notice"), MB_OK);
		return -1;
	}

	ShowWindow(hWnd, SW_SHOW);
	UpdateWindow(hWnd);

	MSG msg;
	while (GetMessage(&msg, NULL, 0, 0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	return 0;
}

LRESULT CALLBACK WinProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	static HCURSOR hCurSor = NULL;
	switch(uMsg)
	{	
	case WM_CREATE:
		{
			if(!pMyEdit)
				pMyEdit = new CMyEdit(hWnd);
			pMyEdit->Init();
			if(!hCurSor)
				hCurSor = LoadCursor(NULL, \
				MAKEINTRESOURCE(IDC_ARROW));
		}
		break;
	case WM_MOUSEMOVE:
		{
			::SetCursor(hCurSor);
			TEventUI event;
			event.Type = UIEVENT_MOUSEMOVE;
			event.wParam = wParam;
			event.lParam = lParam;
			pMyEdit->Event(event);
// 			POINT pt;
// 			pt.x = lParam & 0x0000FFFF;
// 			pt.y = (lParam >> 16) & 0x0000FFFF;
// 			odprintf(TEXT(""));
// 			odprintf(TEXT("x: %d, y: %d"), pt.x, pt.y);
// 			POINT pt1;
// 			pt1.x = 4;
// 			pt1.y = 30;
// 			if(MapWindowPoints(NULL, hWnd, &pt1, 1))
// 				odprintf(TEXT("x: %d, y: %d"), pt1.x, pt1.y);
		}
		break;
	case WM_PAINT:
		{
			PAINTSTRUCT ps;
			HDC hDC = NULL;
			hDC = ::BeginPaint(hWnd, &ps);
			pMyEdit->Render(hDC);
			::EndPaint(hWnd, &ps);
		}
		break;
	case WM_KEYDOWN:
		{
			TEventUI event;
			event.Type = UIEVENT_KEYDOWN;
			event.wParam = wParam;
			event.lParam = lParam;
			pMyEdit->Event(event);
		}
		break;
	case WM_CHAR:
		{
			TEventUI event;
			event.Type = UIEVENT_CHAR;
			event.wParam = wParam;
			event.lParam = lParam;
			pMyEdit->Event(event);
		}
		break;
	case WM_LBUTTONDBLCLK:
		{
			TEventUI event;
			event.Type = UIEVENT_LBUTTONDBCLK;
			event.wParam = wParam;
			event.lParam = lParam;
			pMyEdit->Event(event);
		}
		break;
	case WM_RBUTTONDOWN:
		{
			TEventUI event;
			event.Type = UIEVENT_RBUTTONDOWN;
			event.wParam = wParam;
			event.lParam = lParam;
			pMyEdit->Event(event);
		}
		break;
	case WM_LBUTTONDOWN:
		{
			TEventUI event;
			event.Type = UIEVENT_LBUTTONDOWN;
			event.wParam = wParam;
			event.lParam = lParam;
			pMyEdit->Event(event);
		}
		break;
	case WM_LBUTTONUP:
		{
			TEventUI event;
			event.Type = UIEVENT_LBUTTONUP;
			event.wParam = wParam;
			event.lParam = lParam;
			pMyEdit->Event(event);
		}
		break;
	case WM_SIZE:
		{
			TEventUI event;
			event.Type = UIEVENT_SIZE;
			event.wParam = wParam;
			event.lParam = lParam;
			pMyEdit->Event(event);
		}
		break;
	case WM_CLOSE:
		{
			::DestroyWindow(hWnd);
		}
		break;
	case WM_DESTROY:
		{
			if(pMyEdit)
				delete pMyEdit;
			PostQuitMessage(0);
		}
		break;
	}
	return DefWindowProc(hWnd, uMsg, wParam, lParam);
}